package wnhz.com.business.shareshop.fragment;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.youth.banner.Banner;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.base.MyBaseFragment;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.netcallback.DialogCallback;
import wnhz.com.business.rebateshop.activity.home.ShareFriandActivity;
import wnhz.com.business.shareshop.activity.classify.ClassifGoodsActivity;
import wnhz.com.business.shareshop.activity.home.RebateGoodsDetailsActivityS;
import wnhz.com.business.shareshop.bean.ClassifyHomeBean;
import wnhz.com.business.shareshop.bean.ShareHomDataBean;
import wnhz.com.business.shareshop.ui.SeachActivityS;
import wnhz.com.business.uitls.GlideImageLoader;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.ToastUtils;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * Created by Administrator on 2017-12-13 0013.
 * 共享商城的分类界面
 */

public class ClassifyFragment extends MyBaseFragment implements ActionBarClickListener {
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.sort_rl_man)
    RelativeLayout sortRlMan;
    @BindView(R.id.sort_rl_shipin)
    RelativeLayout sortRlShipin;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.sort_rl_bag)
    RelativeLayout sortRlBag;
    @BindView(R.id.sort_rl_replacement)
    RelativeLayout sortRlReplacement;
    @BindView(R.id.sort_rl_jiaju)
    RelativeLayout sortRlJiaju;
    @BindView(R.id.sort_rl_food)
    RelativeLayout sortRlFood;
    @BindView(R.id.sort_rl_other)
    RelativeLayout sortRlOther;
    Unbinder unbinder;
    private MyBaseActivity activity;
    @BindView(R.id.frag_sort_actionbar)
    TranslucentActionBar frag_sort_actionbar;//标题栏
    @BindView(R.id.recycler_fenlei)
    RecyclerView recycler_fenlei;//顶部分类
    @BindView(R.id.sort_lv_list)
    RecyclerView sort_lv_list;//底部商品
    @BindView(R.id.sort_rl_woman)
    RelativeLayout sortRlWoman;//相机
    @BindView(R.id.banner_rebatehome_banner)
    Banner banner_rebatehome_banner;//首页banner图

    private BaseRVAdapter adapter;
    private List<String> bannerlist = new ArrayList<>();
    private List<ClassifyHomeBean.InfoBean.CatelistBean> cate_list = new ArrayList<>();

    @Override
    protected void handleMsg(Message msg) {

    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_share_classify, container, false);
        ButterKnife.bind(this, view);
        activity = (MyBaseActivity) getActivity();
        frag_sort_actionbar.setData("", 0, null, 1, null, 1, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            frag_sort_actionbar.setStatusBarHeight(getStatusBarHeight());
        }

        initTestGallylist();
        getClassifyData(4);
        return view;
    }

    private void initType(final List<ClassifyHomeBean.InfoBean.CatelistBean> cate_list) {
        recycler_fenlei.setLayoutManager(new GridLayoutManager(getActivity(),4, LinearLayoutManager.VERTICAL,false));
        recycler_fenlei.setAdapter(new BaseRVAdapter(getActivity(),cate_list) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_type;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {

                Glide.with(getActivity()).load(cate_list.get(position).getPic()).into(holder.getImageView(R.id.img_logo));
                holder.setTextView(R.id.text,cate_list.get(position).getClassname());
                holder.getView(R.id.item).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(ClassifGoodsActivity.createIntent(getContext(),position,cate_list.get(position).getCate_id(),cate_list.get(position).getClassname()));
                    }
                });
            }
        });
    }

    /**
     * 分类首页数据
     */
    private void getClassifyData( int goods_type){
       Map<String,Object> maps = new HashMap<>();
       maps.put("token",MyApplication.getToken());
       maps.put("goods_type",goods_type);//1.拼团  2.返利  3.免费  4：共享商城
        XUtil.Post(Url.GOODS_GET_CATE_DATE,maps,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.optString("re"))){
                        Gson gson = new Gson();
                        ClassifyHomeBean classifyHomeBean = gson.fromJson(result,ClassifyHomeBean.class);

                        List<ClassifyHomeBean.InfoBean.BannerBean> bannerBeanList = classifyHomeBean.getInfo().getBanner();

                        if (null != cate_list){
                            cate_list.clear();
                        }
                        if (classifyHomeBean.getInfo().getCatelist().size() > 8){
                            for (int i = 0; i < 8; i++) {
                                cate_list.add(classifyHomeBean.getInfo().getCatelist().get(i));
                            }
                        }else {
                            cate_list.addAll(classifyHomeBean.getInfo().getCatelist());
                        }

                        initType(cate_list);
                        initTestBannerlist(bannerBeanList);
                        initList(classifyHomeBean.getInfo().getGoods());
                    }else if ("-1".equals(jsonObject.optString("re"))){
                        ToastUtils.showToast(getActivity(),getResources().getString(R.string.login_out));
                        MyApplication.getInstance().gotoLoginActivity();
                    }else {
                        ToastUtils.showToast(getActivity(),jsonObject.optString("info"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    /*
    *底部商品列表
    */
    private void initList(final List<ClassifyHomeBean.InfoBean.GoodsBean> goods) {
        adapter = new BaseRVAdapter(activity, goods) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_classify_bottom;
            }//item_rebatehome_bootlist

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
//                holder.setTextView(R.id.item_tv_rebatehome_name, goods.get(position).getGoods_name());
//                holder.setTextView(R.id.item_tv_rebatehome_info, goods.get(position).getGoods_des());
//                holder.setTextView(R.id.item_tv_rebatehome_price, "押金￥" + goods.get(position).getPrice());
//                holder.getView(R.id.item_tv_rebatehome_share).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        //跳转分享页面  (页面标题: 分享商品给好友)
//                        startActivity(ShareFriandActivity.createIntent(getContext(),goods.get(position).getLogo_pic(),"",goods.get(position).getGoods_name(),goods.get(position).getGroup_price()));
//                    }
//                });
                Glide.with(activity).load(goods.get(position).getLogo_pic()).into(holder.getImageView(R.id.share_class_iv));
                RelativeLayout share_classify_bottom_rl = holder.getRelayout(R.id.share_classify_bottom_rl);
                share_classify_bottom_rl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        // TODO: 2018-01-24 0024 点击跳转详情
//                        Intent intent = new Intent(getActivity(),ClassifGoodsActivity.class);
//                        intent.putExtra("title","手机类型");
//                        startActivity(intent);
                        startActivity(RebateGoodsDetailsActivityS.createIntent(activity, goods.get(position).getGoods_id(), goods.get(position).getLogo_pic(), "4"));
                    }
                }
                );
            }
        };

//        adapter.addItemClickListener(new BaseRVAdapter.OnItemClickListener() {
//            @Override
//            public void itemSelect(int position) {
//                startActivity(RebateGoodsDetailsActivityS.createIntent(activity, goods.get(position).getGoods_id(), goods.get(position).getLogo_pic(), "4"));
//            }
//        });
        sort_lv_list.setAdapter(adapter);
    }


    //轮播图片列表地址
    private void initTestBannerlist(List<ClassifyHomeBean.InfoBean.BannerBean> bannerBeanList) {
        for (int i = 0; i < bannerBeanList.size(); i++) {
            bannerlist.add(bannerBeanList.get(i).getPic());
        }
        initBanner();
    }
    /**
     * banner图
     */
    private void initBanner() {
        banner_rebatehome_banner.setImageLoader(new GlideImageLoader());
        banner_rebatehome_banner.setImages(bannerlist);
        banner_rebatehome_banner.start();
        bannerlist.clear();
    }


    private void initTestGallylist() {
        sort_lv_list.setLayoutManager(new GridLayoutManager(activity, 3, LinearLayout.VERTICAL, false));
    }

//    //底部商品列表
//    private void initList(final List<ShareHomDataBean.InfoBean.HotGoodsBean> hot_goods) {
//        adapter = new BaseRVAdapter(activity, hot_goods) {
//            @Override
//            public int getLayoutId(int viewType) {
//                return R.layout.item_classify_fragment_bottom_commodity;
//            }
//
//            @Override
//            public void onBind(BaseViewHolder holder, int position) {
//                Glide.with(activity).load(hot_goods.get(position).getLogo_pic()).into(holder.getImageView(R.id.item_img_rebatehome));
//
//                holder.getImageView(R.id.item_img_rebatehome).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        startActivity(RebateGoodsDetailsActivityS.createIntent(activity, "", "", ""));
//                    }
//                });
//            }
//        };
//        sort_lv_list.setAdapter(adapter);
//    }

    @Override
    public void onLeftClick() {

    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {
        startActivity(new Intent(getActivity(), SeachActivityS.class));
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.sort_rl_man, R.id.sort_rl_shipin, R.id.sort_rl_bag, R.id.sort_rl_replacement, R.id.sort_rl_jiaju, R.id.sort_rl_food, R.id.sort_rl_other})
    public void onViewClicked(View view) {
        switch (view.getId()) {

        }
    }
}
